// pages/module/live/live.js
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import { setTabBar, setTabList } from '../../../utils/util'
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  pindao,
  queryHost,
  getLiveList,
  getLiveClassify,
} from '../../../utils/imapi.js';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 1,
    width: 330,
    sliderLeft: 73,
    sliderOffset: 187.5,
   // name:'competitiveC',
    top: app.globalData.CustomBar,
    tabs: [
     // { "cate_code": "GZ", "name": "关注", "p": "", "is_selected": 0 },
      { "cate_code": "SX", "name": "私享","p": "JD", "is_selected": 0 },
      { "cate_code": "FX", "name": "发现",  "p": "PDD", "is_selected": 1 },
      //{"id":"GY","name":"星益","p":"JD","is_selected":1},
    ],
    zIndex: true,
    title: '星播',
    bgColor: 'white',
    liveList: [],
    img_list: {},
    canLoadMore: true,
    size: 80,
    wsize: 750,
    color: "#6D55ED",
    bottom:app.globalData.isIphonex==1?'100rpx':'10rpx',
    page:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData({
      img_list: img_list.img_list
    })

    this.getLiveClassifyRequest("") //获取分类
    shareFun.hidenShareMenu();
    //this.page = 1




    // 直播记录数据; 默认获取最新的正在直播-即将直播-回放的记录
    const { data } = await getLiveList({ t: 'FX', page_index: 1, page_size: 10 ,is_load_goods:'1'})

    // 京东 商品视频的数据;当审核的时候 使用 商品视频替代
    //const {data} = await pindao({elite_id:130,page_index:1,page_size:10})


    this.canLoadMore = true
    if (data.length > 0) {
      data.forEach(element => {
        element.imgShow = false
        element.is_show = false
      });
    }
    this.showIndex = 0

    this.setData({
      liveList: data
    })





  },
  getLiveClassifyRequest: async function (pid) { // 发送请求,获取频道

    console.log(" 进入 getLiveClassifyRequest")

    let live_plates = []

    try {
      live_plates = await getData('live_plates')
      console.log('live_plates:', live_plates)
    } catch (error) {
      let live_plates_res = await getLiveClassify({ pid: pid })
      live_plates = live_plates_res.data
      setData('live_plates', live_plates_res.data)
    }


    let newTabs = this.data.tabs.concat(live_plates)
    this.setData({
      "tabs": newTabs
    })


  },
  changeTab(event) { // 切换频道
    let index = event.detail.index;
    this.data.tabIndex = index;



  },
  imgload(e) {
    var index = e.detail.index;
    this.data.liveList[index].imgShow = true;
  },
  async onTabClick(event) {
    let index = event.detail.index;
    console.log(index)
    //  const {data} = await pindao({elite_id:130,page_index:1,page_size:10})

    let cate_code = this.data.tabs[index].cate_code


    if (cate_code == 'SX' || cate_code == 'GZ') {

      if (!app.is_authis_auth()) {
        wx.navigateTo({
          url: '/pages/login/login?nav_type=tab',
        })
        return;
      }

    }


    //  获取直播数据
    const { data } = await getLiveList({ t: cate_code, page_index: 1, page_size: 10,is_load_goods:'1' })
    console.log(data)
    this.setData({
      liveList: data,
      tabIndex: index
    })
  },

  async startSearch(e) { //暂不可用
    this.setData({
      tabIndex: 1
    })
    let live_key = e.detail.live_key
    console.log('==search q: ',live_key)
    let { data } = await getLiveList({key_words: live_key, t: 'FX', page_index: 1, page_size: 10,is_load_goods:'1' })
    this.setData({
      liveList: data,
      tabIndex: 1
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(' === is_audit ',app.globalData.is_audit)
    setTabBar.call(this, 1);


   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: async function () {
   wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    setTimeout(function()
    {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },1500);
	
   let cate_code= this.data.tabs[this.data.tabIndex].cate_code
  //  获取直播数据
  const { data } = await getLiveList({ t:cate_code , page_index: 1, page_size: 10,is_load_goods:'1' })
  console.log(data);
  // data[0]['imgShow']=true
  data.forEach((item,index)=>{
	  data[index]['imgShow']=true
  })
  this.setData({
    liveList: data,
    page:1,
  })
wx.hideLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    var that = this;
    if (!this.canLoadMore) return;
    // wx.showLoading({
    //     title: this.page == 1 ? '加载中...' : '加载更多...',
    // });
    this.setData({
      is_load: true
    })
    //const {data} = await pindao({elite_id:130,page_index:this.page+1,page_size:10})
    const { data } = await getLiveList({ t: this.data.tabs[this.data.tabIndex].cate_code, page_index: this.data.page + 1, page_size: 10,is_load_goods:'1' })
    if (data.length > 0) {
      data.forEach(element => {
        element.imgShow = false
        element.is_show = false
      });
    }
    const arr = data;
    if (arr.length > 0) {
     
      this.setData({
        is_load: false,
        liveList: [...this.data.liveList, ...arr],
        page:this.data.page + 1,
      })
    } else {
      this.setData({
        is_load: false,
      })
      this.canLoadMore = false;
    }
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})